package mvc;

import java.util.ArrayList;

import org.geom.Triangle;
import org.geom.Vector3D;
import org.geom.render.IRenderer;
import org.geom.surface.ISurface;
import org.geom.surface.Surface;
import org.geom.surface.TubolarSurface;

public class SurfaceModel  implements ISurface, IModel {
	
	protected Surface surface;


	public SurfaceModel(TubolarSurface surface) {
		this.surface = surface;
	}
	
	//-------------------------------------------------------------
	//------------------------------------------------------------- ISurface
	//-------------------------------------------------------------

	public void addFace(Triangle n) {
		surface.addFace(n);
	}

	public void addFace(Vector3D x, Vector3D y, Vector3D z) {
		surface.addFace(x, y, z);
	}

	public ArrayList<Triangle> getTriangles() {
		if(dirty) {
			surface.sample();
			dirty = false;
		}
		return surface.getTriangles();
	}

	public void sample() {
		surface.sample();
	}

	//-------------------------------------------------------------
	//------------------------------------------------------------- IModel
	//-------------------------------------------------------------
	
	private IModel parent;
	private ArrayList<IRenderer> views = new ArrayList<IRenderer>();
	public boolean dirty = false;
	@Override
	public IModel addParent(IModel parent) {
		this.parent = parent;
		return this;
	}

	
	public void addView(IRenderer v) {
		views.add(v);
	}

	@Override
	public void notifyAllViews() {
		System.out.println("TubolarModel > notitify all views");
		for(IRenderer v : views ) {
			v.render();
			//surface.sample();
			dirty = true;
		}
		if(parent!=null)parent.notifyAllViews();
	}

	@Override
	public Object getData() {
		// TODO Auto-generated method stub
		return surface;
	}


}
